<?php
class Oh_Http
{
	const DEFAULT_LAYOUT = 'Site';
	const KEY_GET_MODULE = 'm';
	const KEY_GET_ACTION = 'a';
	const KEY_GET_CONTROLLER = 'c';
	
    private static $_instance;
    private $controllerName;
    private $actionName;
    private $layoutName;

    
    public static function getInstance()
    {
        if(self::$_instance === null){
            self::$_instance = new self();
        }
        
        return self::$_instance;
    }

    private function __construct()
    {
        $this->initParams();
    }

    public function __get($paramName)
    {
        return $this->$paramName;
    }

    public function __set($paramName, $paramVal)
    {
        $this->$paramName = $paramVal;
    }
    
    public function getParam($paramName)
    {
    	return isset($_REQUEST[$paramName]) ? $_REQUEST[$paramName] : null;
    }

    public function getModuleName()
    {
        return $this->getLayoutName();
    }

    private function initParams()
    {
//     	$this->layoutName = self::DEFAULT_LAYOUT;
        $this->controllerName = 'Index';
        $this->actionName = 'index';
        $router = isset($_GET['a']) ? $_GET['a'] : null; 
        
        if(isset($router) || !empty($router)){
        	$moduleCtrlActionArr = explode('/', $router);
        	if(!empty($moduleCtrlActionArr)){
        		if($moduleCtrlActionArr[0] == 'admin'){
//         			$this->layoutName = 'Admin';
        			if(isset($moduleCtrlActionArr[1]) && !empty($moduleCtrlActionArr[1])){
        				$this->controllerName = ucFirst($moduleCtrlActionArr[1]);
        			}
        			if(isset($moduleCtrlActionArr[2]) && !empty($moduleCtrlActionArr[2])){
        				$this->actionName = $moduleCtrlActionArr[2];
        			}
        		}else{
        			if(isset($moduleCtrlActionArr[0]) && !empty($moduleCtrlActionArr[0])){
        				$this->controllerName = ucFirst($moduleCtrlActionArr[0]);
        			}
        			if(isset($moduleCtrlActionArr[1]) && !empty($moduleCtrlActionArr[1])){
        				$this->actionName = $moduleCtrlActionArr[1];
        			}        			
        		}
        	}
        }
        
        $this->layoutName = str_replace('Index', self::DEFAULT_LAYOUT, ucFirst(basename($_SERVER['SCRIPT_NAME'], '.php')));
//         if(isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])){
//             $queryStringArr = explode('/', $_SERVER['QUERY_STRING']);
//             if(count($queryStringArr)){
//                 if(isset($queryStringArr[0])  && !empty($queryStringArr[0])){
//                     $this->controllerName = ucFirst($queryStringArr[0]);
//                 }
//                 if(isset($queryStringArr[1])  && !empty($queryStringArr[1])){
//                     $this->actionName = $queryStringArr[1];
//                 }
//             }
//         }
//         var_dump(ucFirst(basename($_SERVER['SCRIPT_NAME'], '.php')));
//        	var_dump($this->layoutName);
    }

    public function getBaseUrl()
    {
        var_dump($_SERVER);
    }

    public function getControllerName()
    {
       return $this->controllerName;
    }

    public function getActionName()
    {
        return $this->actionName;
    }

    public function getLayoutName()
    {
        return $this->layoutName;
    }
    
    public function isPost(){
    	
    }
    
    public function isGet(){
    	return !empty($_GET);
    }
}